#ifndef __SMART_PRT_H__
#define __SMART_PRT_H__

#include <iostream>

using namespace std;

template <class Type>
class SmartPtr {
public:
    SmartPtr(Type *ptr) {
        ptr_ = ptr;
        ref_count_ = (unsigned int *)malloc(sizeof(unsigned int));
        ++(*ref_count_);
    }

    SmartPtr(const SmartPtr<Type> &smart_ptr) {
        ptr_ = smart_ptr.ptr_;
        ref_count_ = smart_ptr.ref_count_;
        ++*ref_count_;
    }

    ~SmartPtr() {
        --*ref_count_;
        if (*ref_count_ == 0) {
            delete *ptr_;
            free(ref_count_);
        }
    }

    SmartPtr<Type> & operator = (SmartPtr<Type> &smart_ptr) {
        if (this != &smart_ptr) {
            ptr_ = smart_ptr.ptr_;
            ref_count_ = smart_ptr.ref_count_;
            ++*ref_count_;
        }
        return *this;
    }

private:
    static unsigned int *ref_count_;
    Type *ptr_;
};

#endif

